package cn.ssj.controller;

import cn.ssj.entity.Admin;
import cn.ssj.service.AdminService;
import cn.ssj.utils.RandomValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2017/9/4 0004.
 */
@Controller
@EnableAutoConfiguration
@RequestMapping("/back")
public class AdminController {

    @Autowired
    private AdminService service;

    @RequestMapping("/index")
    public String mainPage(){
        return "back/index";
    }
    /**
     * 登录跳转
     */
    @RequestMapping("/login")
    public  String queryLoginInfo(Admin admin, HttpServletRequest request) throws Exception{
        HttpSession session=request.getSession();
        if(null==admin){
            return "redirect:/login";
        }
        String password=admin.getPassword();
        String name=admin.getName();
        if(null==name||null==password){
            return "redirect:/login";
        }
        String newPassword= DigestUtils.md5DigestAsHex(password.getBytes());
        Admin admin1=service.login(name, newPassword);
        session.setAttribute("admin",admin);
        return "main";
    }

    /**
     * 登录获取验证码
     */
    @RequestMapping("/getLoginValidateCode")
    @ResponseBody
    public String getLoginValidateCode(HttpServletResponse response,
                                       HttpServletRequest request) {
        response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Set-Cookie", "name=value; HttpOnly");//设置HttpOnly属性,防止Xss攻击
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request, response,"imagecode");// 输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 验证码验证
     */
    @RequestMapping(value = "/checkimagecode")
    @ResponseBody
    public String checkTcode(HttpServletRequest request,HttpServletResponse response)
    {
        String validateCode = request.getParameter("validateCode");
        String code = null;
        //1:获取cookie里面的验证码信息
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies)
        {
            if ("imagecode".equals(cookie.getName()))
            {
                code = cookie.getValue();
                break;
            }
        }
        //1:获取session验证码的信息
        //String code1 = (String) request.getSession().getAttribute("");
        //2:判断验证码是否正确
        validateCode = validateCode.toLowerCase();
        code = code.toLowerCase();
        if(!validateCode.isEmpty() && validateCode.equals(code))
        {
            return "ok";
        }
        return "error";
    }

    /**
     * 验证管理员和密码
     */
    @RequestMapping(value = "/checkLoginInfo")
    @ResponseBody
    public String checkLoginInfo(HttpServletRequest request,HttpServletResponse response) throws Exception
    {
        try
        {
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String newPassword=DigestUtils.md5DigestAsHex(password.getBytes());
            Admin admin= service.login(name, newPassword);
            if(null==admin)
            {
                return "error";

            }
            return "ok";
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }
    }
    @RequestMapping("/logout")
    public String Logout(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //清除session中的数据
        request.getSession().invalidate();
        //清除cookie中的数据
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie: cookies){
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return "redirect:/login";
    }

}
